package com.lipiao.food;

import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lipiao.food.Activity.MainActivity;

import java.util.List;

public class FoodRecycleViewAdapter extends RecyclerView.Adapter<FoodRecycleViewAdapter.ContactViewHolder> {
    //MyAdapter的成员变量contactInfoList, 这里被我们用作数据的来源
    private List<FoodBean> foodBeanList;

    //构造函数
    public FoodRecycleViewAdapter(List<FoodBean> foodBeanList) {
        this.foodBeanList = foodBeanList;
    }

    //重写3个抽象方法
    @NonNull
    @Override
    public FoodRecycleViewAdapter.ContactViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.food_card_item, viewGroup, false);
        return new ContactViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final FoodRecycleViewAdapter.ContactViewHolder contactViewHolder, final int i) {
        //i为当前card下标//foodbeanList中包含的都是foodbean类的对象 //通过其get()方法可以获得其中的对象
        //UI操作
        FoodBean foodbean = foodBeanList.get(i);
       // Log.d("sectionsPagerAdapter", "onBindViewHolder: " + foodbean.picUrl);
        Glide.with(MainActivity.getContext()).load(foodbean.picUrl).into(contactViewHolder.foodPic);
        contactViewHolder.foodName.setText(foodbean.foodName);
        contactViewHolder.foodMaterials.setText(foodbean.foodMaterials);

        //监听事件
        //单击
//        contactViewHolder.foodPic
        contactViewHolder.foodCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //触发自定义监听的单击事件
                onItemClickListener.onItemClick(contactViewHolder.itemView,i);
            }
        });
        //长按
        contactViewHolder.foodCard.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //触发自定义监听的长按事件
                onItemClickListener.onItemLongClick(contactViewHolder.itemView,i);

                return  true;//表示此事件已经消费，不会触发单击事件
            }
        });

    }
    //自定义recycleView监听


    @Override
    public int getItemCount() {
        return foodBeanList.size();
    }

    //重点：该泛型必须是 自定义的适配器(如MyAdapter).ViewHolder
    class ContactViewHolder extends RecyclerView.ViewHolder {
        //create the viewHolder class
        protected CardView foodCard;//点击需要的View
        protected ImageView foodPic;
        protected TextView foodName;
        protected TextView foodMaterials;

        public ContactViewHolder(View itemView) {
            super(itemView);
            foodCard =itemView.findViewById(R.id.cv_food);
            foodPic = itemView.findViewById(R.id.iv_food_pic);
            foodName = itemView.findViewById(R.id.tv_food_title);
            foodMaterials = itemView.findViewById(R.id.tv_food_material);
        }
    }

    //有关监听（外部调用）
    public void setOnItemClickListener(FoodRecycleViewAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    private OnItemClickListener onItemClickListener;

    /**
     * 自定义监听回调接口，RecyclerView 的 单击和长按事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }
}
